package client;

// Bot's player class
//Let's say Bot is a thread.

public class BotPlayer implements Runnable {

	public BotPlayer() {
	}
	static BotPlayer bp;
	//Host given to Bot
	static String Host;
	//Port g
	static int Port;
	// Nick given to Bot
	static String Nick = "Andrzej";
	// for connetion
	Client c;
	// BotMoney is a test variable
	int BotBalance = 0, BotBet = 0, BotMoney = 30, BotID = 0;
	// Bot checking if it's can do anything
	boolean canCheck, canRaise, canCall, canFold, canAllin, canBet,
			canExchange, ActionDone;
//Thread is started
	public void run() {
		try {
			System.out.println("Thread has Started");
			MakeAction();
			while (true) {
				BotAvailableAction(MessageFromServer(c.receive()));
				MakeAnAction();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Errors while connecting");
		}
	}
	//We can run bot from Console :D
	public static void main(String[] args) throws Exception {
		Host = args[0];
		try {
			Port = Integer.parseInt(args[1]);
		} catch (NumberFormatException e) {
			System.out.println("Wrong port given");
		}
		try {
			Nick = args[2];
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("Wrong Bot Nick");
			Nick = "Andrzej";
		}
		(new Thread(new BotPlayer())).start();
	}

	// Bot Connects to the server
	public void MakeAction() throws Exception {
		c = new Client(Host, Port, bp);
		c.connect();
		c.send("nick " + Nick);
		c.send("start");
	}

	// Bot is doing one action
	public void MakeAnAction() {
		if (ActionDone == false) {
			if (canBet) {
				c.send("bet " + BotMoney);
				System.out.println("Made Bet");
				ActionDone = true;
			}
		}
		if (ActionDone == false) {
			if (canCheck) {
				c.send("check");
				System.out.println("Made Check");
				ActionDone = true;
			}
		}
		if (ActionDone == false) {
			if (canFold) {
				c.send("fold");
				System.out.println("Made Fold");
				ActionDone = true;
			}
		}
		if (ActionDone == false) {
			if (canAllin) {
				c.send("allin");
				System.out.println("Made Allin");
				ActionDone = true;
			}
		}
		if (ActionDone == false) {
			if (canRaise) {
				c.send("raise " + BotMoney);
				System.out.println("Made Raise");
				ActionDone = true;
			}
		}
		if (ActionDone == false) {
			if (canCall) {
				c.send("call");
				System.out.println("Made Call");
				ActionDone = true;
			}
		}
	}
	//Bot is reading information from server
	//For now it reads info about what it can do
	public void BotAvailableAction(String[] command) {
		switch (command[2]) {
		case "playerinfo":
			BotBalance = Integer.parseInt(command[4]);
			BotBet = Integer.parseInt(command[5]);
			BotID = Integer.parseInt(command[3]);
			System.out.println("BotBalance: " + BotBalance + "" + " BotBet: "
					+ BotBet + "" + " " + " BotID: " + BotID + "" + " ");
			break;
		case "permission":
			if (command[3].equals("1")) {
				canBet = true;
				ActionDone = false;
				System.out.println(canBet + "");
			} else
				canBet = false;
			if (command[4].equals("1")) {
				canRaise = true;
				ActionDone = false;
			} else
				canRaise = false;

			if (command[5].equals("1")) {
				canCheck = true;
				ActionDone = false;
			} else
				canCheck = false;
			if (command[6].equals("1")) {
				canCall = true;
				ActionDone = false;
			} else
				canCall = false;
			if (command[7].equals("1")) {
				canAllin = true;
				ActionDone = false;
			} else
				canAllin = false;
			if (command[8].equals("1")) {
				canFold = true;
				ActionDone = false;
			} else
				canFold = false;
			if (command[9].equals("1")) {
				canExchange = true;
				ActionDone = false;
			} else
				canExchange = false;
			break;
		case "cards":
			break;
		}

	}
	//Same as in Client Class
	public static String[] MessageFromServer(String command) {
		System.out.println(command);
		String[] s = command.split(" ");
		return s;
	}
}

